//
//  DailyBottomView.swift
//  MM2
//
//  Created by 李成明 on 2022/1/2.
//

import UIKit

class DailyBottomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var likeClickClosure: ((DailyModel, Bool) -> Void)?
    private var replyClickClosure: ((DailyModel) -> Void)?
    var model: DailyModel? {
        didSet {
            updateUI()
        }
    }
    
    private lazy var shareBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "front_page_forward_icon"), for: .normal)
        view.setTitle("分享", for: .normal)
        view.titleLabel?.font = .systemFont(ofSize: 16)
        view.setTitleColor(mainColor, for: .normal)
        view.imageView?.contentMode = .scaleAspectFit
        
        view.imageEdgeInsets = UIEdgeInsets(top: 10, left: -60, bottom: 10, right: 0)
        view.titleEdgeInsets = UIEdgeInsets(top: 0, left: -80, bottom: 0, right: 0)
        return view
    }()
    
    private lazy var commontBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "front_page_commont_icon"), for: .normal)
        view.titleLabel?.font = .systemFont(ofSize: 16)
        view.setTitleColor(mainColor, for: .normal)
        view.imageView?.contentMode = .scaleAspectFit
        
        view.imageEdgeInsets = UIEdgeInsets(top: 10, left: -60, bottom: 10, right: 0)
        view.titleEdgeInsets = UIEdgeInsets(top: 0, left: -80, bottom: 0, right: 0)
        view.addTarget(self, action: #selector(replyClickAction), for: .touchUpInside)
        return view
    }()
    
    private lazy var likeBtn: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = .systemFont(ofSize: 16)
        view.setTitleColor(mainColor, for: .normal)
        view.imageView?.contentMode = .scaleAspectFit
        
        view.imageEdgeInsets = UIEdgeInsets(top: 10, left: -60, bottom: 10, right: 0)
        view.titleEdgeInsets = UIEdgeInsets(top: 0, left: -80, bottom: 0, right: 0)
        view.addTarget(self, action: #selector(likeClickAction), for: .touchUpInside)
        return view
    }()
    
    private func setupUI() {
        addsubViews([shareBtn, commontBtn, likeBtn])
        
        let width = (kScreenWidth - 32)/3 + 8
        shareBtn.frame = CGRect(x: 0, y: 0, width: width, height: 48)
        commontBtn.frame = CGRect(x: width, y: 0, width: width, height: 48)
        likeBtn.frame = CGRect(x: width*2, y: 0, width: width, height: 48)
    }
    
    private func updateUI() {
        guard let model = model else { return }
        
        likeBtn.setImage(model.like ? UIImage(named: "daily_liked_icon") : UIImage(named: "front_page_like_icon"), for: .normal)
        commontBtn.setTitle("\(model.replyNum)", for: .normal)
        likeBtn.setTitle("\(model.likeNum)", for: .normal)
    }
}

extension DailyBottomView {
    @discardableResult
    func likeClick(_ closure: ((DailyModel, Bool) -> Void)?) -> DailyBottomView {
        likeClickClosure = closure
        return self
    }
    
    @discardableResult
    func replyClick(_ closure: ((DailyModel) -> Void)?) -> DailyBottomView {
        replyClickClosure = closure
        return self
    }
    
    @objc private func likeClickAction() {
        guard let model = model, model.like == false else { return }
        model.like = !model.like
        likeBtn.setTitle("", for: .normal)
        likeBtn.setImage(model.like ? UIImage(named: "daily_liked_icon") : UIImage(named: "front_page_like_icon"), for: .normal)
        likeClickClosure?(model, model.like)
    }
    
    @objc private func replyClickAction() {
        guard let model = model else { return }
        replyClickClosure?(model)
    }
}
